<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use app\mill\model\Give as GiveModel;
use app\wh\model\Material as MaterialModel;
use think\Db;

/**
 * 车辆模型
 */
class Plan extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_order_plan';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 获取计划环节
    public static function getPlanSort(){
        //获取计划环节
        $sort = GiveModel::$give_status_search;
        //$sort['-1'] = '下单';
        //unset($sort[0]);
        $sort['0'] = '开平';
        ksort($sort);

        return $sort;
    }

    //显示各生产线的特殊工艺
    public static $plan_give_status_arr = [
        2   =>  ['knife'=>'刀数'],//折弯显示已做刀数
        3   =>  ['weld'=>'焊缝','slot'=>'刨槽'],//焊接显示已做焊缝、刨槽
    ];

    // 表格制作
    //print 0:编辑模式 1:打印模式
    public static function fromTabale($data = [],$print = 0){


        $table_head = [
            [''],
            ['计划'],
            ['计划完成时间'],
            ['备注'],
        ];

        $table_data = [];

        $plan_hidden = '';
        $plan_list = $data['plan_list'];
        foreach ($plan_list as $k=>$v){
            $k = $v['give_status'];
            $checked = empty($v['is_true'])?'':"checked";
            if($v['give_status'] == -1){
                $plan_hidden = MaterialModel::inputToText("plan_list[{$k}][is_true]",$v['is_true'],$print,'','hidden')
                    .MaterialModel::inputToText("plan_list[{$k}][give_status]",$v['give_status'],$print,'','hidden')
                    .MaterialModel::inputToText("plan_list[{$k}][title]",$v['title'],$print,'','hidden')
                    .MaterialModel::inputToText("plan_list[{$k}][plan_time]",$v['plan_time'],$print,'','hidden')
                    .MaterialModel::inputToText("plan_list[{$k}][note]",$v['note'],$print,'','hidden')
                    ;
                continue;
            }
            $table_data[] = [
                "[0:1]
                {$plan_hidden}
                <label class='css-input css-checkbox css-checkbox-primary css-checkbox-sm css-checkbox-rounded'>
            <input type='checkbox' name='plan_list[{$k}][is_true]' value='1' {$checked}><span></span></label>"
                .MaterialModel::inputToText("plan_list[{$k}][give_status]",$v['give_status'],$print,'','hidden'),
                "[0:1]{$v['title']}
                ".MaterialModel::inputToText("plan_list[{$k}][title]",$v['title'],$print,'','hidden'),
                "[0:1]
                ".MaterialModel::inputToText("plan_list[{$k}][plan_time]",$v['plan_time'],$print,'','date',"plan_time{$k}"),
                "[0:1]
                ".MaterialModel::inputToText("plan_list[{$k}][note]",$v['note'],$print),
            ];
            $plan_hidden = '';
        }


        return ['head'=>$table_head,'data'=>$table_data];
    }

    //更新排产计划单
    public static function updatePlan($order = [],$give_status = -1,$plan_time = 0,$start_time = 0,$end_time = 0,$note = ''){

        if(empty($order['order_id']))$order['order_id'] = $order['id'];
        //-1:排产总计划单 0:来料计划单 1~7:各生产线计划单
        $plan = Db::name('mill_order_plan')->where(['order_id'=>$order['order_id'],'give_status'=>$give_status])->find();
        if(empty($plan)){
            $add = [
                'order_id'          =>  $order['order_id'],
                'order_pid'         =>  $order['order_pid'],
                'archives_sn'       =>  $order['archives_sn'],
                'give_status'       =>  $give_status,
                'note'              =>  $note,
                'plan_time'         =>  $plan_time,
                'start_time'        =>  $start_time,
                'end_time'          =>  $end_time,
                'create_time'       =>  time(),
            ];

            //是否取消该工艺的排产计划
            if(isset($order['is_true']))$add['is_true'] = $order['is_true'];
            if(isset($order['plan_title']))$add['title'] = $order['plan_title'];

            Db::name('mill_order_plan')->insert($add);
        }
        else{
            $add = [];
            if(!empty($note))$add['note'] = $note;
            if(!empty($plan_time))$add['plan_time'] = $plan_time;
            if(!empty($start_time) && $plan['start_time'] == 0)$add['start_time'] = $start_time;
            if(!empty($end_time)){
                $add['end_time'] = $end_time;
                //判断完成时间是否超过计划时间
                if($plan['plan_time'] > 0 && $end_time > ($plan['plan_time'] + 86400))$add['is_timeout'] = 0;
                //更新计划单状态
                $add['status'] = 1;
            }
            //是否取消该工艺的排产计划
            if(isset($order['is_true']))$add['is_true'] = $order['is_true'];

            if(!empty($add) && $plan['status'] == 0){
                Db::name('mill_order_plan')->where(['id'=>$plan['id']])->update($add);
            }
        }

    }

    //更新下单管理
    public static function updatePlace($order = []){

        //获取工程所有批次的面积和数量
        $archives = Db::name('mill_order_archives')
            ->alias('a')
            ->field('SUM(a.row_area) as row_area,SUM(a.number) as number')
            ->leftJoin('mill_order o','o.id=a.order_id')
            ->where(['o.order_sn_off'=>$order['order_sn_off']])
            ->find();

        //根据订单编号获取对应的下单信息
        $place = Db::name('mill_order_place')->where(['order_sn_off'=>$order['order_sn_off']])->find();

        if(!empty($place)){
            $add = [
                'archives_row_area'         =>  $archives['row_area'],
                'archives_number'           =>  $archives['number'],
            ];

            Db::name('mill_order_place')->where(['id'=>$place['id']])->update($add);
        }

    }

}
